package com.cz.nongyetongv5.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.cz.nongyetongv5.R;
import com.cz.nongyetongv5.bean.RegisterPic;
import com.github.chrisbanes.photoview.PhotoView;

import java.util.List;

public class MarkPicActivity extends Activity {
    private RelativeLayout rl_back;

    private ViewPager viewpager;
    private ViewPagerAdapter adapter;
    private TextView tv_name;
    private List<RegisterPic> registerPics;
    private int position;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mark_pic);
        Intent intent = getIntent();
        position = intent.getIntExtra("position", 0);
        registerPics = (List<RegisterPic>) getIntent().getSerializableExtra("photos");
        rl_back = findViewById(R.id.rl_back);
        rl_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        viewpager = findViewById(R.id.viewpager);
        tv_name = findViewById(R.id.tv_name);

        adapter = new ViewPagerAdapter();
        viewpager.setAdapter(adapter);
        viewpager.setCurrentItem(position);
        tv_name.setText(registerPics.get(position).getTitle());
        viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int p) {
                tv_name.setText(registerPics.get(p).getTitle());
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }

    public class ViewPagerAdapter extends PagerAdapter {


        @Override
        public int getCount() {
            return registerPics.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            String imgUrl = registerPics.get(position).getUrl();
            RelativeLayout view = (RelativeLayout) LayoutInflater.from(MarkPicActivity.this).inflate(R.layout.viewpager_pic_item, null);
            PhotoView img = (PhotoView) view.findViewById(R.id.photo_view);
            Glide.with(MarkPicActivity.this)
                    .load(imgUrl)
                    .into(img);
            container.addView(view);
            return view;

        }
    }
}
